﻿using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class Operators : System.Web.UI.Page
{
    ServerObject mServerObject = null;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (mServerObject == null) mServerObject = new ServerObject();
        if (Utils.AutentificatedUser)
        {
            if (!IsPostBack)
            {
                ShowPanel(operatorsPanel.ID);
            }
        }
        else
        {
            ShowPanel(string.Empty);
        }
    }

    private void ShowPanel(string panelName)
    {
        #region Hide panels

        emptyPanel.Visible = false;
        operatorsPanel.Visible = false;

        addNewOperatorPanel.Visible = false;
        editOperatorPanel.Visible = false;

        #endregion Hide panels

        try
        {
            #region Get Panel Name

            switch (panelName)
            {
                case "operatorsPanel":
                    operatorsPanel.Visible = true;

                    FillOperatorsGridView();

                    break;

                case "addNewOperatorPanel":
                    addNewOperatorPanel.Visible = true;
                    {
                        DataTable genderList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.GenderList);
                        Utils.FillSelector(addNewOperatorGenDDL, genderList, "Name", "Code"); 
                    }

                    break;

                case "editOperatorPanel":
                    editOperatorPanel.Visible = true;
                    {
                        DataTable genderList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.GenderList);
                        Utils.FillSelector(editOperatorGenDDL, genderList, "Name", "Code"); 
                    }

                    break;

                case "emptyPanel":
                    emptyPanel.Visible = true;
                    break;

                default:
                    emptyPanel.Visible = true;
                    break;
            }

            #endregion Get Panel Name

        }
        catch (Exception ex)
        {
            Utils.InfoText(this, "Atentie! Eroare in sistem!", ex.Message);
        }
    }

    protected void FillOperatorsGridView()
    {
        DataTable operators = Utils.ModuleCredits().GetOperatorsList();
        operatorsGridView.DataSource = operators;
        operatorsGridView.DataBind();
    }

    private void ClearNewOperatorsPanel()
    {
        addNewOperatorGenDDL.SelectedIndex = -1;

        addNewOperatorNumeTextBox.Text = string.Empty;
        addNewOperatorPrenumeTextBox.Text = string.Empty;
        addNewOperatorSpecializareTextBox.Text = string.Empty;
        addNewOperatorAdresaTextBox.Text = string.Empty;
        addNewOperatorTelefonFixTextBox.Text = string.Empty;
        addNewOperatorTelefonGSMTextBox.Text = string.Empty;
        addNewOperatorEmailTextBox.Text = string.Empty;
        addNewOperatorOrganizatieTextBox.Text = string.Empty;
    }

    private void ClearEditOperatorPanel()
    {
        editOperatorGenDDL.SelectedIndex = -1;

        editOperatorNumeTextBox.Text = string.Empty;
        editOperatorPrenumeTextBox.Text = string.Empty;
        editOperatorSpecializareTextBox.Text = string.Empty;
        editOperatorAdresaTextBox.Text = string.Empty;
        editOperatorTelefonFixTextBox.Text = string.Empty;
        editOperatorTelefonGSMTextBox.Text = string.Empty;
        editOperatorEmailTextBox.Text = string.Empty;
        editOperatorOrganizatieTextBox.Text = string.Empty;
    }

    protected void addNewTrainingButton_Click(object sender, EventArgs e)
    {
        ClearNewOperatorsPanel();
        ShowPanel(addNewOperatorPanel.ID);
    }

    protected void operatorsGridView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        operatorsGridView.PageIndex = e.NewPageIndex;
        FillOperatorsGridView();
    }

    protected void operatorsGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            for (int i = 0; i < e.Row.Cells.Count - 1; i++)
            {
                e.Row.Cells[i].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.operatorsGridView, "Select$" + e.Row.RowIndex);
            }
        }
    }

    protected void operatorsGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;
        int operatorID = 0;
        int.TryParse(operatorsGridView.Rows[index].Cells[0].Text, out operatorID);

        string nume = operatorsGridView.Rows[index].Cells[1].Text;
        string prenume = operatorsGridView.Rows[index].Cells[2].Text;

        if (Utils.ModuleCredits().DeleteOperator(operatorID))
        {
            ShowPanel(operatorsPanel.ID);
        }
        else
        {
            Utils.InfoText(this, "Atentie", " { " + nume + " " + prenume + " } - nu a fost sters. Verificati conectiunea cu internet ");
        }

    }

    protected void operatorsGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearEditOperatorPanel();
        ShowPanel(editOperatorPanel.ID);

        GridViewRow row = operatorsGridView.SelectedRow;

        string operatorID = row.Cells[0].Text;
        if (!operatorID.Equals("&nbsp;"))
            editOperatorIDHiddenField.Value = operatorID;

        string opartorNume = row.Cells[1].Text;
        if (!opartorNume.Equals("&nbsp;"))
            editOperatorNumeTextBox.Text = opartorNume;

        editOperatorPrenumeTextBox.Text = row.Cells[2].Text;

        if (!row.Cells[3].Text.Equals(string.Empty))
            editOperatorGenDDL.SelectedValue = row.Cells[3].Text;

        string specializare = row.Cells[5].Text;
        if (!specializare.Equals("&nbsp;"))
        editOperatorSpecializareTextBox.Text = specializare;

        string adresa = row.Cells[6].Text;
        if (!adresa.Equals("&nbsp;"))
            editOperatorAdresaTextBox.Text = adresa;

        string telFix = row.Cells[7].Text;
        if (!telFix.Equals("&nbsp;"))
            editOperatorTelefonFixTextBox.Text = telFix;

        string telGsm = row.Cells[8].Text;
        if (!telGsm.Equals("&nbsp;"))
            editOperatorTelefonGSMTextBox.Text = telGsm;

        string email = row.Cells[9].Text;
        if (!email.Equals("&nbsp;"))
            editOperatorEmailTextBox.Text = email;

        string organ = row.Cells[10].Text;
        if (!organ.Equals("&nbsp;"))
            editOperatorOrganizatieTextBox.Text = organ;
    }

    protected void addNewOperatorSaveButton_Click(object sender, EventArgs e)
    {
        string nume = addNewOperatorNumeTextBox.Text.Trim();
        string prenume = addNewOperatorPrenumeTextBox.Text.Trim();

        int gen = 0;
        int.TryParse(addNewOperatorGenDDL.SelectedValue.ToString(), out gen);

        string specializare = addNewOperatorSpecializareTextBox.Text.Trim();
        string adresa = addNewOperatorAdresaTextBox.Text.Trim();
        string telefonFix = addNewOperatorTelefonFixTextBox.Text.Trim();
        string telefonGSM = addNewOperatorTelefonGSMTextBox.Text.Trim();
        string email = addNewOperatorEmailTextBox.Text.Trim();
        string organizatie = addNewOperatorOrganizatieTextBox.Text.Trim();

        if (Utils.ModuleCredits().AddNewOperator(nume, prenume, gen, specializare, adresa, telefonFix, telefonGSM, email, organizatie))
        {
            ShowPanel(operatorsPanel.ID);
        }
        else
        {
            Utils.InfoText(this, "Atentie", " { " + nume + " " + prenume + " } - nu a fost salvat. Verificati conectiunea cu internet ");
        }
    }

    protected void addNewOperatorCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(operatorsPanel.ID);
    }

    protected void editOperatorSaveButton_Click(object sender, EventArgs e)
    {
        int operatorID = 0;
        int.TryParse(editOperatorIDHiddenField.Value , out operatorID);

        string nume = editOperatorNumeTextBox.Text.Trim();
        string prenume = editOperatorPrenumeTextBox.Text.Trim();

        int gen = 0;
        int.TryParse(editOperatorGenDDL.SelectedValue.ToString(), out gen);

        string specializare = editOperatorSpecializareTextBox.Text.Trim();
        string adresa = editOperatorAdresaTextBox.Text.Trim();
        string telefonFix = editOperatorTelefonFixTextBox.Text.Trim();
        string telefonGSM = editOperatorTelefonGSMTextBox.Text.Trim();
        string email =  editOperatorEmailTextBox.Text.Trim();
        string organizatie = editOperatorOrganizatieTextBox.Text.Trim();

        if (Utils.ModuleCredits().UpdateOperator(operatorID, nume, prenume, gen, specializare, adresa, telefonFix, telefonGSM, email, organizatie))
        {
            ShowPanel(operatorsPanel.ID);
        }
        else
        {
            Utils.InfoText(this, "Atentie", " { " + nume + " " + prenume + " } - nu a fost salvat. Verificati conectiunea cu internet ");
        }
    }

    protected void editOperatorCancelButton_Click(object sender, EventArgs e)
    {
        ShowPanel(operatorsPanel.ID);
    }
}